#include "Geometric/Face.hpp"

namespace mif
{
	
	int Face::mFaceCount = 0;
	
	Face::Face()
	{
		//EMPTY	
	}
	
	Face::Face(Vertex * pA, Vertex * pB, Vertex * pC) :
		mID(mFaceCount++)
	{
		mTriangle.a = pA;
		mTriangle.b = pB;
		mTriangle.c = pC;
		
		mNeighborhood.A = mNeighborhood.B = mNeighborhood.C = nullptr;
		
		mColor.r = mColor.g = mColor.b = 255;
		mColor.a = 0;
		
	}
	
	Face::~Face()
	{
		//EMPTY
	}

	bool Face::isVertex(Vertex * pVertex) const{
			if(mTriangle.a == pVertex || mTriangle.b == pVertex || mTriangle.c == pVertex)
				return true;
				
			return false;
	}
	
	const int Face::GetID() const
	{
		return mID;
	}
	
	const Triangle Face::GetTriangle() const
	{
		return mTriangle;
	}
			
	Neighborhood Face::GetNeighborhood() const
	{
		return mNeighborhood;
	}
	
	Face* Face::GetNeighborhoodWithVertices(Vertex* pA,Vertex* pB) const
	{

		if(mNeighborhood.A->isVertex(pA) && mNeighborhood.A->isVertex(pB))
			return mNeighborhood.A;
		
		if(mNeighborhood.B->isVertex(pA) && mNeighborhood.B->isVertex(pB))
			return mNeighborhood.B;
		
		if(mNeighborhood.C->isVertex(pA) && mNeighborhood.C->isVertex(pB))
			return mNeighborhood.C;
		
		std::cout << "Error : Neighbor Face not found (in neighborhood detection) " << std::endl;
		exit(-1);
	}

	Vertex* Face::getNextVertex(Vertex* pVertex) const
	{

		if(mTriangle.a == pVertex)
			return mTriangle.b;
		
		if(mTriangle.b == pVertex)
			return mTriangle.c;
		
		if(mTriangle.c == pVertex)
			return mTriangle.a;
		
		std::cout << "Error : In Finding Next Vertex" << std::endl;
		exit(-1);

	}

	RGBA Face::GetColor() const
	{
		return mColor;
	}
	
	void Face::SetNeighborhood(Face * pFacePointer, int pEdgeInd)
	{
		/*
					a
					/\
		   2<-(B)  /  \  (C)->3
				  /    \
				 /______\
				c		 b
					(A)->1
		*/
		switch(pEdgeInd){
			case 1:
				mNeighborhood.A = pFacePointer;
				break;
			case 2:
				mNeighborhood.B = pFacePointer;
				break;
			case 3:
				mNeighborhood.C = pFacePointer;
				break;
			default:
				break;
		}
		
	}
			
	void Face::SetColor(RGBA pColor)
	{
		mColor = pColor;
	}
	
}
